package com.fh.wanandroid.ui.knowledge.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fh.wanandroid.R;
import com.fh.wanandroid.app.Constants;
import com.fh.wanandroid.base.BaseFragment;
import com.fh.wanandroid.ui.articledetail.ArticleDetailActivity;
import com.fh.wanandroid.ui.knowledge.contract.KnowledgeListContract;
import com.fh.wanandroid.ui.knowledge.presenter.KnowledgeListPresenter;
import com.fh.wanandroid.ui.mainpager.MainPagerFragment;
import com.fh.wanandroid.ui.mainpager.adaper.ArticleRvAdapter;
import com.fh.wanandroid.ui.mainpager.bean.FeedArticleListData;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class KnowledgeListFragment extends BaseFragment<KnowledgeListContract.Presenter> implements KnowledgeListContract.View, OnRefreshListener, OnLoadmoreListener,ArticleRvAdapter.ItemClickListener{


    @BindView(R.id.knowledge_list_recycler_view)
    RecyclerView knowledgeListRecyclerView;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.drawer_layout)
    FrameLayout drawerLayout;

    ArticleRvAdapter articleRvAdapter;

    int cid ;

    List<FeedArticleListData.DataBean.DatasBean> articleList;

    @Override
    public KnowledgeListPresenter getmPresenter() {
        return new KnowledgeListPresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null) {
            mPresenter.setView(this);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_knowledge_list;
    }

    public static KnowledgeListFragment getInstance(String title, int cid) {

        KnowledgeListFragment fragment = new KnowledgeListFragment();
        Bundle args = new Bundle();
        args.putString(Constants.ARG_PARAM1, title);
        args.putInt(Constants.ARG_PARAM2, cid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        knowledgeListRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        knowledgeListRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        srl.setOnRefreshListener(this);
        srl.setOnLoadmoreListener(this);

    }



    @Override
    public void initData() {

        Bundle bundle = getArguments();
        cid = bundle.getInt(Constants.ARG_PARAM2, 0);
        if (cid == 0) {
            return;
        }
         articleList = new ArrayList<>();
         articleRvAdapter  = new ArticleRvAdapter(getContext(),articleList);
         knowledgeListRecyclerView.setAdapter(articleRvAdapter);
         articleRvAdapter.setmItemClickListener(this);

        srl.autoRefresh();

    }

    @Override
    public void loadData() {



    }


    @Override
    public void refreshKnowledgeArticle(FeedArticleListData articleList) {

        this.articleList.clear();

        this.articleList.addAll(articleList.getData().getDatas());

        articleRvAdapter.notifyDataSetChanged();

        finishRefresh();

    }

    @Override
    public void appendKnowledgeArticle(FeedArticleListData articleList) {

        int oldNum = this.articleList.size();

        this.articleList.addAll(articleList.getData().getDatas());

        articleRvAdapter.notifyItemRangeChanged(oldNum,this.articleList.size());

        finishLoadMore();

    }

    @Override
    public void loadDataFail(String msg) {

        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {

        mPresenter.loadMoreData(cid);

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {

        mPresenter.loadInitData(cid);

    }

    @Override
    public void onClick(int position) {


        Intent intent = new Intent(getContext(), ArticleDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id", articleList.get(position).getId());
        bundle.putString("title", articleList.get(position).getTitle());
        bundle.putString("link", articleList.get(position).getLink());
        intent.putExtras(bundle);
        startActivity(intent);

    }


    /**
     * 结束刷新动作
     *
     * @return 是否结束刷新动作
     */
    private boolean finishRefresh() {
        if (srl.isRefreshing()) {
            srl.finishRefresh();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 结束加载更多动作
     */
    private void finishLoadMore() {
        if (srl.isLoading()) {
            srl.finishLoadmore();
        }
    }

}
